/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.enterprise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.EsmsProductCloud;
import org.springblade.modules.enterprise.entity.EsmsTalentService;
import org.springblade.modules.enterprise.service.IEsmsTalentServiceService;
import org.springblade.modules.enterprise.vo.EsmsTalentServiceVO;
import org.springblade.modules.enterprise.wrapper.EsmsTalentServiceWrapper;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.service.IDictService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 人才服务需求表 控制器
 *
 * @author BladeX
 * @since 2021-07-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("enterprise/esmstalentservice")
@Api(value = "人才服务需求表", tags = "人才服务需求表接口")
public class EsmsTalentServiceController extends BladeController {

	private final IEsmsTalentServiceService esmsTalentServiceService;
	private final IDictService iDictService;
	private final IDictService dictService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入esmsTalentService")
	public R<EsmsTalentServiceVO> detail(EsmsTalentService esmsTalentService) {
		EsmsTalentService detail = esmsTalentServiceService.getOne(Condition.getQueryWrapper(esmsTalentService));
		return R.data(EsmsTalentServiceWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 人才服务需求表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入esmsTalentService")
	public R<IPage<EsmsTalentServiceVO>> list(EsmsTalentService esmsTalentService, Query query) {
		IPage<EsmsTalentService> pages = esmsTalentServiceService.page(Condition.getPage(query), Condition.getQueryWrapper(esmsTalentService));
		return R.data(EsmsTalentServiceWrapper.build().pageVO(pages));
	}

	/**
	 * 分页获取当前账号关联的服务信息列表
	 */
	@GetMapping("/unique-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入esmsTalentService")
	public R<IPage<EsmsTalentServiceVO>> uniqueList(EsmsTalentService esmsTalentService, Query query) {
		BladeUser user = AuthUtil.getUser();
		esmsTalentService.setEnterpriseAccount(user.getAccount());
		QueryWrapper<EsmsTalentService> queryWrapper = Condition.getQueryWrapper(esmsTalentService);
		queryWrapper.orderByDesc("create_time");
		IPage<EsmsTalentService> pages = esmsTalentServiceService.page(Condition.getPage(query), queryWrapper);
		return R.data(EsmsTalentServiceWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义分页 人才服务需求表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入esmsTalentService")
	public R<IPage<EsmsTalentServiceVO>> page(EsmsTalentServiceVO esmsTalentService, Query query) {
		IPage<EsmsTalentServiceVO> pages = esmsTalentServiceService.selectEsmsTalentServicePage(Condition.getPage(query), esmsTalentService);
		return R.data(pages);
	}

	/**
	 * 新增 人才服务需求表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入esmsTalentService")
	public R save(@Valid @RequestBody EsmsTalentService esmsTalentService) {
		return R.status(esmsTalentServiceService.save(esmsTalentService));
	}

	/**
	 * 修改 人才服务需求表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入esmsTalentService")
	public R update(@Valid @RequestBody EsmsTalentService esmsTalentService) {
		return R.status(esmsTalentServiceService.updateById(esmsTalentService));
	}

	/**
	 * 新增或修改 人才服务需求表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入esmsTalentService")
	public R submit(@Valid @RequestBody EsmsTalentService esmsTalentService) {
		BladeUser user = AuthUtil.getUser();
		if(esmsTalentService.getEnterpriseAccount()==null||esmsTalentService.getEnterpriseAccount()==""){
			esmsTalentService.setEnterpriseAccount(user.getAccount());
		}
		return R.status(esmsTalentServiceService.saveOrUpdate(esmsTalentService));
	}


	/**
	 * 删除 人才服务需求表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(esmsTalentServiceService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 服务类型
	 */
	@GetMapping("serviceTypeList")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "服务类型")
	public R serviceTypeList() {
		BladeUser user = AuthUtil.getUser();
		QueryWrapper<Dict> dickQueryWrapper = new QueryWrapper<>();
		dickQueryWrapper.eq("is_deleted", 0).eq("dict_value", "服务类型").eq("dict_key", "-1");
		Dict wxies = iDictService.getOne(dickQueryWrapper);
		QueryWrapper<Dict> dickListQueryWrapper = new QueryWrapper<>();
		dickListQueryWrapper.eq("parent_id", wxies.getId());
		List<Dict> wxieslist = iDictService.list(dickListQueryWrapper);
		return R.data(wxieslist);
	}

	/**
	 * 根据服务类型查找对应的子节点
	 */
	@GetMapping("sonServiceTypeList")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "根据服务类型查找对应的子节点")
	public R sonServiceTypeList(Dict dick) {
		QueryWrapper<Dict> dickQueryWrapper = new QueryWrapper<>();
		dickQueryWrapper.eq("is_deleted", 0).eq("parent_id", dick.getId());
		List<Dict> wxies = iDictService.list(dickQueryWrapper);
		return R.data(wxies);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detailForId")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dict")
	public R detail(String id) {
		Dict dict = new Dict();
		if (id != null && id != "") {
			dict.setId(Long.parseLong(id));
		}
		Dict detail = dictService.getOne(Condition.getQueryWrapper(dict));
		return R.data(detail.getDictValue());
	}

	/**
	 * 查询状态改变未查阅的数据，并设置为已查询
	 */
	@GetMapping("readService")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "根据服务类型查找对应的子节点")
	public R readService(EsmsTalentService esmsTalentService) {
		QueryWrapper<EsmsTalentService> esmsTalentServiceQueryWrapper = new QueryWrapper<>();
		esmsTalentServiceQueryWrapper.eq("read_status", 1).eq("enterprise_account",esmsTalentService.getEnterpriseAccount()).eq("service_type",esmsTalentService.getServiceType());
		List<EsmsTalentService> esmsTalentServiceList = esmsTalentServiceService.list(esmsTalentServiceQueryWrapper);
//		for (EsmsTalentService ServiceList:esmsTalentServiceList) {
//			ServiceList.setReadStatus(2);
//			esmsTalentServiceService.saveOrUpdate(ServiceList);
//		}
		return R.data(esmsTalentServiceList);
	}
	/**
	 * 查询状态改变未查阅的数据，并设置为已查询
	 */
	@GetMapping("readDetail")
	@ApiOperationSupport(order = 17)
	@ApiOperation(value = "根据服务类型查找对应的子节点")
	public R readDetail(EsmsTalentService esmsTalentService) {
		QueryWrapper<EsmsTalentService> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",esmsTalentService.getId());
		EsmsTalentService detail = esmsTalentServiceService.getOne(queryWrapper);
		detail.setReadStatus(2);
		esmsTalentServiceService.saveOrUpdate(detail);
		return R.data(detail);

	}

}
